﻿/*
 * @Author: KnifeZ 
 * @Date: 2019-01-26 14:33:05 
 * @Last Modified by:   KnifeZ 
 * @Last Modified time: 2019-01-26 14:33:05 
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using System.Text;
using Xamarin.Forms.PlatformConfiguration;

namespace Xarmarin.GoldFinger.Engine.Core
{
    public class CoreEngine
    {
        public static readonly string ConnectionString = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "lite.db");

        public void CloseApp()
        {

        }
    }
    public static class EnumExt
    {

        /// <summary>
        /// 扩展方法，获得枚举的Description
        /// </summary>
        /// <param name="value">枚举值</param>
        /// <param name="nameInstend">当枚举没有定义DescriptionAttribute,是否用枚举名代替，默认使用</param>
        /// <returns>枚举的Description</returns>
        public static string GetDescription(this Enum value, bool nameInstend = true)
        {
            Type type = value.GetType();
            string name = Enum.GetName(type, value);
            if (name == null)
            {
                return null;
            }
            FieldInfo field = type.GetField(name);
            DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
            if (attribute == null && nameInstend == true)
            {
                return name;
            }
            return attribute == null ? null : attribute.Description;
        }
        /// <summary>
        /// 获取下一个枚举值
        /// 没有下一个返回-9999
        /// </summary>
        /// <param name="value"></param>
        /// <param name="nameInstend"></param>
        /// <returns></returns>
        public static long GetNextValue(this Enum value, bool nameInstend = true)
        {
            var flag = false;
            Type type = value.GetType();
            if (!Enum.IsDefined(type, value))return 0;

            Array enumValues = Enum.GetValues(type);
            foreach (var item in enumValues)
            {
                if (flag)
                {
                    return Convert.ToInt64(item);
                }
                if (value.ToString() == item.ToString())
                {
                    flag = true;
                }
            }
            return -9999;
        }

        /// <summary>
        /// 把枚举转换为键值对集合
        /// </summary>
        /// <param name="enumType">枚举类型</param>
        /// <returns>以枚举值为key，枚举文本为value的键值对集合</returns>
        public static Dictionary<Int32, String> EnumToDictionary(Type enumType)
        {
            if (!enumType.IsEnum)
            {
                throw new ArgumentException("传入的参数必须是枚举类型！", "enumType");
            }
            Dictionary<Int32, String> enumDic = new Dictionary<int, string>();
            Array enumValues = Enum.GetValues(enumType);
            foreach (Enum enumValue in enumValues)
            {
                Int32 key = Convert.ToInt32(enumValue);
                String value = enumValue.GetDescription();
                enumDic.Add(key, value);
            }
            return enumDic;
        }
    }
}
